<?php
//$proId=$_REQUEST['products'];
require_once 'includes/class.phpmailer.php';
session_start();
$sessid=session_id();

 //echo $pthumb;

?>
<style type="text/css">
	span#add-wishlist { color:#0000CC; padding:5px; font-size:12px; }	
</style>
<script language="javascript1.2">
function updateQty(id,avqty)
{
//alert(id);
//alert(avqty);
	var qtxt='qty'+id;
	var qtext = parseInt($('#qty'+id).val());
	
//alert(qtext);
        
        if(qtext=="" || isNaN(qtext) )
	{
	    //alert("Please enter valid Quantity");
	    $('#update_error'+id).html('Please enter valid Quantity');
	    $("#update_error"+id).fadeOut(2000);
	    return false;
	}
	else if(qtext>avqty)
	{
	     var pos = $('#upd'+id).position();
	     var nleft = parseInt(pos.left);
	     var ntop = parseInt(pos.top);
	     $('#msg_box_lp').css({'left': nleft, 'top':ntop});
	     //$('#login_box').fadeIn('slow');
	     $('#msg_box_lp').show();
	     $('#trans_box1').html('You entered in Qty that is larger than what is available. Please reduce the Qty. <br /><input type="button" class="refer_btn" value="Close" onclick="$(\'#msg_box_lp\').hide();" style="margin-left:5px;" />');
	     //$('#update_error'+id).html('Please enter valid Quantity');
	    return false;
	}
	else if(qtext==0)
	{
		//alert(qtext);
		window.location="delete.php?id="+id;
	}
	else
	{
	var url = "<?php echo $base_url;?>ajax_updqty.php";
	      $.post(url,
	      { 'qty':qtext,'id':id},
	      function(data)
	      {
		
		if(data=='Record updates successfully')
		{
		  $('#update_error'+id).html(data);
		  $("#update_error"+id).fadeOut(2000);
		  window.location='index.php?cart';
		}
		else if(data=='')
		  window.location='index.php';
		else
		  window.location='index.php?'+data;
		      
	      },
	      'html'
	      );
	}
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function savedraft()
{
   alert("abc");
    var url = "<?php echo $base_url;?>addtodraft.php";
      $.post(url,
      { },
      function(data)
      {
	//alert(data);
	if(trim(data)=='success')
	{
	  $('#update_error').html('Record updated successfully');
	  $("#update_error").fadeOut(2000);
	  
	}
	else if(data=='')
	  window.location='index.php';
	
	      
      },
      'html'
      );
   
}

function set_online(objvalue,objTempID)
{
      var url = "<?php echo $base_url;?>addtolocal.php";
      $.post(url,
      { 'val':objvalue,'id':objTempID},
      function(data)
      {
	//alert(data);
	if(trim(data)=='success')
	{
	  $('#update_error').html('Record updated successfully');
	  $("#update_error").fadeOut(2000);
	}
	else if(data=='')
	  window.location='index.php';
      },
      'html'
      );
//location.href="addtolocal.php?val="+objvalue +"&id="+objTempID;
}

</script>
<div id="msg_box_lp" class="srlink_popup2" style="display:none;">
	<div class="srlinkpp2_text" id="trans_box1">
	  
	</div>
</div>
<table width="100%">
<tr>
<td>
<div style="width: 950px; float: left; vertical-align: top;">
	<a name="crt" id="crt"></a>
	<p style="text-align:center;"><img src="images/viewcart.png" /></p>
	
	<div class="fulldiv">
		<p class="pro_ptag">&nbsp;&nbsp;Your cart for <?php if(isset($_SESSION['user_id_sess'])){?> <?php echo getUser($_SESSION['user_id_sess'])?> (<a href="javascript:void(0);" onclick="window.location='index.php?signout&redi=<?php echo $_SESSION['user_id_sess']?>'" class="bluelink">If it's not you click here</a>) <?php } ?></p>
	</div>
        <?php if($_SESSION['dmsg'])
	{?>
	  <div class="fulldiv">
	    <p class="pro_ptag"><strong><?php echo $_SESSION['dmsg'];$_SESSION['dmsg']="";?></strong></p>
	  </div>
	  <?php }?>
	  <?php if($_SESSION['cart_msg'])
	{?>
	  <div class="fulldiv">
	    <p class="pro_ptag"><strong><?php echo $_SESSION['cart_msg'];$_SESSION['cart_msg']="";?></strong></p>
	  </div>
	  <?php }?>
	  <div class="fulldiv" >
	     <p id="update_error"></p>
	  </div>
	<div class="fullbox_out">		
		<div class="fulldiv">
			<div class="fullbox_top"></div>
			<div class="fullbox_repeat">			
				
				<div class="sellpro_table">
					<div class="sellpro_row1">
						<strong class="orangehead">Qty</strong>
					</div>
					<div class="sellpro_row2">
						<strong class="orangehead">Product Description</strong>
					</div>
					<div class="sellpro_row2">
						<strong class="orangehead">Delivery Options</strong>
					</div>
					<div class="sellpro_row3">
						<strong class="orangehead">Price</strong>							
					</div>
				</div>
				<?php
		$i=0;
		$k=1;
		$shipping =0;
		if(($_SESSION['viewcart']!="") && (!empty($_SESSION['user_id_sess'])) )
		{
		    $sqlCart="select * from tempcart where user_id='".$_SESSION['user_id_sess']."'";
		}
		else
		{
		   $sqlCart="select * from tempcart where session_id='".$sessid."'";
		} 
		 $resCart=mysql_query($sqlCart);
	  while($rowCart=mysql_fetch_array($resCart))
	  {
		$i=1;
		
		$itemId=$rowCart['itemid'];
		$sqlSelItem="SELECT * FROM tbl_items WHERE item_id='$itemId'";
		$rstSelItem=mysql_query($sqlSelItem);
		$rowSelItem=mysql_fetch_array($rstSelItem);
		$proId=$rowSelItem['product_id'];	
		$productOwner=$rowSelItem['seller_id'];
		$productDesc=$rowSelItem['product_description'];
		$productCategory=$rowSelItem['category_id'];
		$productPrice=$rowSelItem['price'];
		$bargainAmount=$rowSelItem['price'];
		$tempid=$rowCart['tempid'];
		$sales_tax==0;
		
		//$sqlSelProd="SELECT * FROM tbl_product p left join tbl_handling_time h on h.handling_time_id=p.shipping_id left join tbl_shipping_service s on p.courior_id=s.shipping_id  WHERE product_id='$proId'";
		$sqlSelProd="SELECT * FROM tbl_product WHERE product_id='$proId'";
		//echo $sqlSelProd;
		$rstSelProd=mysql_query($sqlSelProd);
		$rowSelProd=mysql_fetch_array($rstSelProd);
		$productOOwner=$rowSelProd['posted_by'];
		$localPickup=$rowSelProd['localPickup'];
		$quantity=$rowSelProd['quantity'];
		//echo $quantity;
		$ownername=getUser($productOOwner);
		$pro_condition=$rowSelProd['pro_condition'];
		$productTitle=$productName=$rowSelProd['product_name'];	
		$postedDate=$rowSelProd['posted_date'];
		$bargainDate = $rowSelProd['bargain_duration'];
		$sellingType=$rowSelProd['selling_type'];
		//$shipping = number_format($rowSelProd['shipping'],2);
		//$shipping =0;
		$shippCost=$rowSelProd['shipping'];

		if($rowCart['isWhole']=='1')
		{
			$sqlSelProd="SELECT * FROM tbl_wholeseller_product p WHERE product_id='$itemId'";
			$rstSelProd=mysql_query($sqlSelProd);
			$rowSelProd=mysql_fetch_array($rstSelProd);
		}
		$localpk="";
		$shippk="";
		$dispk="";
		//echo $localPickup;
		if($localPickup==1)
		{
		   $localpk="checked='checked'";
		   $shippk="";
		   
		}
		else
		{
		    $localpk="";
		    $shippk="checked='checked'";
		    $dispk="disabled='disabled'";
		}
		
		if($sellingType=='both')
		{
		       $shippingAvailability="Free";
		       
		       if((int)$rowCart['is_online']==1)
		       {
			       if(((int)$rowSelProd['shipping'])>=0)
			       {
				       $shipping=$rowSelProd['shipping']+$shipping;
			       }
			       $online='checked=true';
			       $offline="";			
		       }
		       else
		       {
			       //$shipping=$shipping;
			       $offline='checked=true';
			       $online="";
			       $shippCost="";
		       }	
		       
		       $pickupAvailability="<input type='radio' onclick='set_online(0,'".$tempid."');' $offline />Local<input type='radio' onclick='return set_online(1,$tempid);' $online />Online ";
			       
		}
	       else if($sellingType=='online')
		{
		       $shippingAvailability="Free";
		       $pickupAvailability="Not available";
		       if(((int)$rowSelProd['shipping'])>=0)
		       {
			       $shipping=$rowSelProd['shipping']+$shipping;
		       }
		}
	        else if($sellingType=='local')
		{
		       $shippingAvailability="Not available";
		       $pickupAvailability="Free";
		       //$shipping =0;
		       $shippCost="";
		       
		       $sqlPost1="update tempcart set is_online='0',verify_code='".rand(1000000000,9999999999)."' where tempid='".$rowCart['tempid']."' ";
		       $rstPost1=mysql_query($sqlPost1) or die(mysql_error());	
		       //$shipping=$shipping;
		}
		
		
		if(((int)$rowSelProd['sales_tax'])>=0)
		{
			$sales_tax=$rowSelProd['sales_tax']+$sales_tax;
		}
		$total=($rowSelProd['product_price']+number_format((number_format($rowCart['sales_tax'],2)*($rowSelProd['product_price'])/100),2))*(int)$rowCart['qty']+$total;
	 
	  ?>
	                        <div class="sellpro_table">
				    <p class="pro_ptag" style="color:red;" id="update_error<?php echo $rowCart['tempid'];?>">
					
				    </p>
				</div>
				<div class="sellpro_table">
					
					<div class="sellpro_row1">
						<p class="pro_ptag"><input name="qty<?php echo $rowCart['tempid'];?>" id="qty<?php echo $rowCart['tempid'];?>" type="text" size="2" value="<?php echo $rowCart['qty'];?>" style="width:15px;" class="inputbg"/> of <?php echo $quantity?></p>
						<p class="pro_ptag"><a href="javascript:" id="upd<?php echo $rowCart['tempid'];?>" class="bluelink" onclick="return updateQty('<?php echo $rowCart['tempid'];?>','<?php echo $quantity?>');">update</a></p>
						<p class="pro_ptag"><a href="delete.php?id=<?php echo $rowCart['tempid'];?>" class="bluelink">remove</a></p>
					</div>
					
					<div class="sellpro_row2">	
							<p class="sellpro_head pro_ptag" style="margin:5px 0px 5px 0px;">
							<a href='index.php?products=<?php echo $itemId; ?>' title='<?php echo $productTitle; ?>' class="sellpro_head"><?php echo $productName?> </a>
							</p>
							<p class="pro_ptag"><i>Condition - <?php echo $pro_condition?></i></p>
							<p class="pro_ptag"><i>product owner - <?php echo $ownername?></i></p>
							<p class="pro_ptag"><i>local pick up - <font style="color:#000000;"><?php echo $pickupAvailability;?></font></i></p>					
					</div>
					
					<div class="sellpro_row2">
						<p class="pro_ptag"><input type="radio" name="tt<?php echo $k?>" onclick='return set_online(1,<?php echo $tempid?>);' <?php echo $shippk?>  id="" />&nbsp;&nbsp;<label>Shipping</label></p>
						<p class="pro_ptag"><input type="radio" name="tt<?php echo $k?>" onclick='return set_online(0,<?php echo $tempid?>);'  <?php echo $localpk?> <?php echo $dispk?> id=""/>&nbsp;&nbsp;<label>Local Pick up</label></p>
					</div>
					
					<div class="sellpro_row3">
						<p class="redtxt pro_ptag">$<?php echo $productPrice?></p>							
					</div>
				</div>
				<?php $k++; }
				
				$total1=$total+$shipping;
				if($i==0)
				{
				?>
				   <div class="sellpro_row4_in" style="text-align:center;">
					Your Cart is empty!
				    </div>
				<?php
				}
				?>
				<div class="sellpro_table">
					<p class="pro_ptag">
					<?php
					if($i==1 && !empty($_SESSION['user_id_sess']))
					{
					?>	
						<input class="yellow_btn" type="button" value="Save for later" onclick="return savedraft();" />
					<?php
					}
					?>
					</p>
				</div>
							
			</div>
			<div class="fullbox_bot"></div>
			
			<div class="sellpro_table">
				<div class="sellpro_row4">
					<img src="images/shipping.png" />
				</div>	
				<div class="sellpro_row4">
				<?php
				if($i==1)
				{
				?>
					<div class="sellpro_row4_in">
						<div class="sellpro_row4_left">
							Product price:
						</div>
						<div class="sellpro_row4_right">
							<font >$<?php echo number_format($total,2);?></font>
						</div>
					</div>
					
					<div class="sellpro_row4_in">
						<div class="sellpro_row4_left">
							Shipping:
						</div>
						<div class="sellpro_row4_right">
							$<?php echo number_format($shipping,2);?>
							<?php $_SESSION['shipping'] = $shipping; ?>
						</div>
					</div>
					
					<!--<div class="sellpro_row4_in">
						<div class="sellpro_row4_left">
							Sales tax CA:
						</div>
						<div class="sellpro_row4_right">
							$<?php// echo number_format($sales_tax,2)?>
						</div>
					</div>-->
					
					<div class="sellpro_row4_in">
						<div class="sellpro_row4_left">
							Sub-total:
						</div>
						<div class="sellpro_row4_right">
							<font class="redtxt"><?php echo number_format($total1,2);?></font>
						</div>
					</div>
					
					<div class="sellpro_row4_in" style="border-top:1px solid #767676;">
						<div class="fulldiv pro_ptag">
							<div class="sellpro_row4_left">
								<a href="index.php" class="bluelink">Continue Shopping</a>
							</div>
							<div class="sellpro_row4_right">
								<input type="submit" value="Checkout" class="yellow_btn" style="float:right;" onclick="window.location='amazon_payment.php'">
							</div>
						</div>
					</div>
				
				<?php
				}
				?>
				</div>
			     
			</div>
			
		</div>
	
	</div>

	<div class="mypg_left_out">
		<div class="fulldiv">
			<div class="shourtbox" style="margin:5px 0px 5px 0px">
			Get it faster by choosing pick-up<br /><br />
			Safe and secure checkouts
			through Amazon Payments
			</div>
		</div>
		<div class="fulldiv">
			<div class="cartico_94"></div>
		</div>
	</div>
	
</div>
</td>
</tr>
</table>

		 
